package ch.hslu.weather;

import java.util.HashMap;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;

public class WeatherAPI {

	private HashMap<String, Drawable> hashMap;
	private HashMap<String, Drawable> hashMapNight;
	private Context context;

	public WeatherAPI(Context context) {
		this.context = context;

		hashMap = new HashMap<String, Drawable>();
		hashMap.put("clear", context.getResources().getDrawable(
				R.drawable.sunny));
		hashMap.put("cloudy", context.getResources().getDrawable(
				R.drawable.cloudy));
		hashMap.put("fog", context.getResources().getDrawable(R.drawable.fog));
		hashMap
				.put("haze", context.getResources()
						.getDrawable(R.drawable.haze));
		hashMap.put("light rain", context.getResources().getDrawable(
				R.drawable.lightrain));
		hashMap.put("mostly cloudy", context.getResources().getDrawable(
				R.drawable.cloudy));
		hashMap.put("overcast", context.getResources().getDrawable(
				R.drawable.overcast));
		hashMap.put("partly cloudy", context.getResources().getDrawable(
				R.drawable.partcloudy));
		hashMap
				.put("rain", context.getResources()
						.getDrawable(R.drawable.rain));
		hashMap.put("rain showers", context.getResources().getDrawable(
				R.drawable.rainshowers));
		hashMap.put("showers", context.getResources().getDrawable(
				R.drawable.shower));
		hashMap.put("thunderstorm", context.getResources().getDrawable(
				R.drawable.thunderstorm));
		hashMap.put("chance of showers", context.getResources().getDrawable(
				R.drawable.chanceofshower));
		hashMap.put("chance of snow", context.getResources().getDrawable(
				R.drawable.snow));
		hashMap.put("chance of storm", context.getResources().getDrawable(
				R.drawable.chanceofthunderstorm));
		hashMap.put("mostly sunny", context.getResources().getDrawable(
				R.drawable.sunny));
		hashMap.put("partly sunny", context.getResources().getDrawable(
				R.drawable.sunny));
		hashMap.put("scattered showers", context.getResources().getDrawable(
				R.drawable.rainshowers));
		hashMap.put("sunny", context.getResources().getDrawable(
				R.drawable.sunny));

		/*
		 * HashMapNight mit Nacht-Icons
		 */
		hashMapNight = new HashMap<String, Drawable>();
		hashMapNight.put("clear", context.getResources().getDrawable(
				R.drawable.night_clear));
		hashMapNight.put("cloudy", context.getResources().getDrawable(
				R.drawable.night_cloudy));
		hashMapNight.put("fog", context.getResources().getDrawable(
				R.drawable.night_foggy));
		hashMapNight.put("haze", context.getResources().getDrawable(
				R.drawable.night_foggy));
		hashMapNight.put("light rain", context.getResources().getDrawable(
				R.drawable.lightrain));
		hashMapNight.put("mostly cloudy", context.getResources().getDrawable(
				R.drawable.night_cloudy));
		hashMapNight.put("overcast", context.getResources().getDrawable(
				R.drawable.overcast));
		hashMapNight.put("partly cloudy", context.getResources().getDrawable(
				R.drawable.night_partcloudy));
		hashMapNight.put("rain", context.getResources().getDrawable(
				R.drawable.night_rain));
		hashMapNight.put("rain showers", context.getResources().getDrawable(
				R.drawable.night_showers));
		hashMapNight.put("showers", context.getResources().getDrawable(
				R.drawable.night_showers));
		hashMapNight.put("thunderstorm", context.getResources().getDrawable(
				R.drawable.night_thunderstorm));
		hashMapNight.put("chance of showers", context.getResources()
				.getDrawable(R.drawable.chanceofshower));
		hashMapNight.put("chance of snow", context.getResources().getDrawable(
				R.drawable.snow));
		hashMapNight.put("chance of storm", context.getResources().getDrawable(
				R.drawable.chanceofthunderstorm));
		hashMapNight.put("mostly sunny", context.getResources().getDrawable(
				R.drawable.sunny));
		hashMapNight.put("partly sunny", context.getResources().getDrawable(
				R.drawable.night_partcloudy));
		hashMapNight.put("scattered showers", context.getResources()
				.getDrawable(R.drawable.night_rain));
		hashMapNight.put("sunny", context.getResources().getDrawable(
				R.drawable.night_clear));
	}

	public Drawable getImage(String input, String time) {

		Log.i("Input Time is", time);
		int intHour = 12;
		try {
			String strHour = time.split(" ")[1].split(":")[0];
			intHour = Integer.valueOf(strHour);
		} catch (ArrayIndexOutOfBoundsException e) {
		}

		Log.i("Output Hour is", String.valueOf(intHour));

		if (hashMapNight.containsKey(input.toLowerCase())) {
			if (intHour < 6 || intHour > 18) {
				return hashMapNight.get(input.toLowerCase());
			} else {
				return hashMap.get(input.toLowerCase());
			}
		}
		return context.getResources().getDrawable(R.drawable.na);
	}
}
